﻿/*
获取页面脚本错误
alert(ErrorTrace.join('\n\n'));
Embeb stacktrace.js file before the end of the head tag
if a url is specific, the error will automaticlly sent to the url using a post method.
*/
(function (win) {

    var StackTrace = function () { }
      , proto = StackTrace.prototype
      , stacktrace

    // make error traces visitable outside the closure
    win.ErrorTrace = [];

    // set a url to pesist your error traces
    proto.url = '';

    // run a supervisor
    proto.track = function () {
        var that = this;

        return function (msg, url, line) {
            ErrorTrace.push(msg + '@' + line);
            that.post(msg, url, line);
        }
    };

    // post data to backend
    proto.post = function (msg, url, line) {
        var xhr, params;

        params = 'url=' + url +
          '&message=' + msg +
          '&line=' + line +
          '&ua=' + navigator.userAgent;

        xhr = typeof win.XMLHttpRequest === 'undefined' ?
          new ActiveXObject('Msxml2.XMLHTTP') :
          new XMLHttpRequest();

        xhr.open('POST', this.url, true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.send(params);
    };

    stacktrace = new StackTrace();

    // record error traces
    win.onerror = stacktrace.track();

})(window);